﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SeparateProjects
{
    public partial class Pro_Admin_Modify_Information : Form
    {
        #region  构造函数传递学号
        /// <summary>
        /// 构造函数传参数
        /// </summary>
        /// <param name="UserName"></param>
        public Pro_Admin_Modify_Information(int UserName)
        {
            InitializeComponent();
            this.txt_UserName.Text = UserName.ToString();
            this.txt_UserName.Enabled = false;
        }
        #endregion、
        #region  加载学生信息
        /// <summary>
        /// 预加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pro_Admin_Modify_Information_Load(object sender, EventArgs e)
        {
            string str = @"Data Source = DESKTOP-28VSNS5;Initial Catalog = Training_Project;
                           Uid = Lv;Pwd = 123456;";
            SqlConnection coon = new SqlConnection(str);
            try
            {
                grade();
                coon.Open();
                var sql = string.Format(@"select S.Sid,L.Password,S.Stu_StuInfo_Name,S.Stu_StuInfo_Age,
                                          S.Stu_StuInfo_Sex,S.Stu_StuInfo_Grade,S.Stu_StuInfo_Card,S.Stu_StuInfo_Address,
                                          S.Stu_StuInfo_Birthday from Stu_Login L
                                          inner join Stu_StuInfo S on(S.Sid = L.Sid)
                                          where L.UserName  = '{0}'",txt_UserName.Text.Trim());
                var comm = new SqlCommand(sql, coon);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    this.txt_Password.Text =Convert.ToString(dr["Password"]);
                    this.txt_Name.Text = Convert.ToString(dr["Stu_StuInfo_Name"]);
                    this.txt_Age.Text = Convert.ToString(dr["Stu_StuInfo_Age"]);
                    this.txt_ID.Text = Convert.ToString(dr["Stu_StuInfo_Card"]);
                    this.txt_Address.Text = Convert.ToString(dr["Stu_StuInfo_Address"]);
                    this.dateTime.Text = Convert.ToString(dr["Stu_StuInfo_Birthday"]);
                    this.lbl_Sid.Text = Convert.ToString(dr["Sid"]);
                    int Sex = Convert.ToInt32(dr["Stu_StuInfo_Sex"]);
                    int Grade = Convert.ToInt32(dr["Stu_StuInfo_Grade"]);
                    if (Sex.Equals(1))
                    {
                        this.radio_man.Checked = true;
                    }
                    else if (Sex.Equals(2))
                    {
                        this.radio_woman.Checked = true;
                    }
                    switch (Grade)
                    {
                        case 1:
                            this.cho_Class.Text = "大一";
                            break;
                        case 2:
                            this.cho_Class.Text = "大二";
                            break;
                        case 3:
                            this.cho_Class.Text = "大三";
                            break;
                    }

                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常，请联系管理员" +ex.Message);
            }
            finally
            {
                coon.Close();
            }
        }
        #endregion
        #region  输出年级
        /// <summary>
        /// 输出年级
        /// </summary>
        public void grade()
        {
             string str = @"Data Source = DESKTOP-28VSNS5;Initial Catalog = Training_Project;
                           Uid = Lv;Pwd = 123456;";
            SqlConnection coon = new SqlConnection(str);
            var sql = "select * from Grade";
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(sql, coon);
            adp.Fill(ds);
            this.cho_Class.DisplayMember = "Grede_Name";
            this.cho_Class.ValueMember = "Stu_StuInfo_Grade";
            this.cho_Class.DataSource = ds.Tables[0];
        }
        #endregion
        #region 点击修改用户信息
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Modify_Click(object sender, EventArgs e)
        {
            var Password = this.txt_Password.Text;
            var Name = this.txt_Name.Text;
            var Age = this.txt_Age.Text;
            var ID = this.txt_ID.Text;
            var Birthday = this.dateTime.Text;
            var Address = this.txt_Address.Text;
            var Sid = this.lbl_Sid.Text.Trim();
            int Sex = -1;
            if (this.radio_man.Checked)
            {
                Sex = 1;
            }
            else if (this.radio_woman.Checked)
            {
                Sex = 2;
            }
            int Grade = -1;
            switch (this.cho_Class.Text)
            {
                case "大一":
                    Grade = 1;
                    break;
                case "大二":
                    Grade = 2;
                    break;
                case "大三":
                    Grade = 3;
                    break;
                default:
                    MessageBox.Show("年级输入值有误，请直接选择", "提示信息",
                                                       MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    break;
            }
            Legal();
            string str = @"Data Source = DESKTOP-28VSNS5;
                            Initial Catalog = Training_Project;
                            Uid = Lv;
                            Pwd = 123456";
            var coon = new SqlConnection(str);
            try
            {
                coon.Open();
                var Login_sql = string.Format(@"update Stu_Login set Password = '{0}'
                                       where UserName = {1}", Password, this.txt_UserName.Text.Trim());
                var sql = string.Format(@"update Stu_StuInfo set Stu_StuInfo_Name = '{0}',Stu_StuInfo_Age = '{1}',
	                                    Stu_StuInfo_Sex = '{2}',Stu_StuInfo_Grade = '{3}',Stu_StuInfo_Card = '{4}',
                                        Stu_StuInfo_Address = '{5}',Stu_StuInfo_Birthday ='{6}'
	                                    where (Stu_StuInfo.Sid = '{7}')",Name,Age,Sex,Grade,ID,Address,Birthday,Sid);
                var conm = new SqlCommand(Login_sql, coon);
                var comm = new SqlCommand(sql, coon);
                int num = conm.ExecuteNonQuery();
                int number = comm.ExecuteNonQuery();
                if (num > 0 && number > 0)
                {
                    DialogResult dr = MessageBox.Show("修改成功", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dr.Equals(DialogResult.OK))
                    {
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("添加失败，请稍后再试", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常，请联系管理员" +ex.Message);
            }
            finally
            {
                coon.Close();
            }
            
        }
        #endregion
        #region  修改检测是否合法
        public void Legal()
        {
            if (string.IsNullOrWhiteSpace(this.txt_Password.Text))
            {
                MessageBox.Show("密码不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Password.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.txt_Name.Text))
            {
                MessageBox.Show("姓名不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Name.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.txt_Age.Text))
            {
                MessageBox.Show("年龄不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Age.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.cho_Class.Text))
            {
                MessageBox.Show("年级不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.cho_Class.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.txt_ID.Text))
            {
                MessageBox.Show("身份证不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_ID.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.txt_Address.Text))
            {
                MessageBox.Show("家庭住址不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Address.Focus();
            }
        }
        #endregion
    }
}
